package sendcloud

import (
	"errors"
	"fmt"
	"net/url"
	"strconv"
)

const (
	kLabelList   = "http://api.sendcloud.net/apiv2/label/list"
	kLabelGet    = "http://api.sendcloud.net/apiv2/label/get"
	kLabelAdd    = "http://api.sendcloud.net/apiv2/label/add"
	kLabelDelete = "http://api.sendcloud.net/apiv2/label/delete"
	kLabelUpdate = "http://api.sendcloud.net/apiv2/label/update"
)

type Label struct {
	GMTCreated  string `json:"gmtCreated"`
	GMTtUpdated string `json:"gmtUpdated"`
	LabelId     int    `json:"labelId"`
	LabelName   string `json:"labelName"`
}

// --------------------------------------------------------------------------------
type LabelListRsp struct {
	Result     bool   `json:"result"`
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		DataList []*Label `json:"dataList"`
		Total    int      `json:"total"`
		Count    int      `json:"count"`
	} `json:"info"`
}

// GetLabelList 查询(批量查询)邮件标签
// query    	string  否   模糊查询的关键词
// start        int     否   查询起始位置, 取值区间 [0-], 默认为 0
// limit        int     否   查询个数, 取值区间 [0-100], 默认为 100
func (this *Client) GetLabelList(query string, start, limit int) (result *LabelListRsp, err error) {
	param := url.Values{}

	if query != "" {
		param.Add("query", query)
	}
	if start >= 0 {
		param.Add("start", fmt.Sprintf("%d", start))
	}
	if limit >= 1 {
		param.Add("limit", fmt.Sprintf("%d", limit))
	}

	if err = this.doRequest(kLabelList, param, &result); err != nil {
		return nil, err
	}

	if result.Result == false {
		return nil, errors.New(fmt.Sprintf("%d-%s", result.StatusCode, result.Message))
	}

	return result, err
}

// --------------------------------------------------------------------------------
type GetLabelRso struct {
	Result     bool   `json:"result"`
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		Data *Label `json:"data"`
	} `json:"info"`
}

// GetLabel 查询邮件标签详细信息
// labelId   string   是    邮件标签Id
func (this *Client) GetLabel(labelId int) (result *GetLabelRso, err error) {
	param := url.Values{}
	param.Add("labelId", strconv.Itoa(labelId))

	if err = this.doRequest(kLabelGet, param, &result); err != nil {
		return nil, err
	}

	if result.Result == false {
		return nil, errors.New(fmt.Sprintf("%d-%s", result.StatusCode, result.Message))
	}

	return result, err
}

// --------------------------------------------------------------------------------
type AddLabelRsp struct {
	Result     bool   `json:"result"`
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		Data *Label `json:"data"`
	} `json:"info"`
}

// AddLabel 添加模板
// labelName     string    是   邮件标签名称
func (this *Client) AddLabel(labelName string) (result *AddLabelRsp, err error) {
	param := url.Values{}
	param.Add("labelName", labelName)

	if err = this.doRequest(kLabelAdd, param, &result); err != nil {
		return nil, err
	}

	if result.Result == false {
		return nil, errors.New(fmt.Sprintf("%d-%s", result.StatusCode, result.Message))
	}

	return result, err
}

// --------------------------------------------------------------------------------
type DeleteLabelRsp struct {
	Result     bool   `json:"result"`
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		Count int `json:"count"`
	} `json:"info"`
}

// DeleteLabel 删除模板
// labelId     int    是   	需要删除的标签ID
func (this *Client) DeleteLabel(labelId int) (result *DeleteLabelRsp, err error) {
	param := url.Values{}
	param.Add("labelId", strconv.Itoa(labelId))

	if err = this.doRequest(kLabelDelete, param, &result); err != nil {
		return nil, err
	}

	if result.Result == false {
		return nil, errors.New(fmt.Sprintf("%d-%s", result.StatusCode, result.Message))
	}

	return result, err
}

// --------------------------------------------------------------------------------
type UpdateLabelRsp struct {
	Result     bool   `json:"result"`
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		Count int `json:"count"`
	} `json:"info"`
}

// UpdateTemplate 修改模板信息
// labelId     	  int      是  需要修改的标签ID
// labelName      string   是  需要修改的标签名称
func (this *Client) UpdateLabel(labelId int, labelName string) (result *UpdateLabelRsp, err error) {
	param := url.Values{}
	param.Add("labelId", strconv.Itoa(labelId))
	param.Add("labelName", labelName)

	if err = this.doRequest(kLabelUpdate, param, &result); err != nil {
		return nil, err
	}

	if result.Result == false {
		return nil, errors.New(fmt.Sprintf("%d-%s", result.StatusCode, result.Message))
	}

	return result, err
}
